package com.zl.learning.io.test.test;

/**
 * Created by zhaolei on 2017/5/2.
 */

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class Program2 {
    static private final byte message[] = {83, 111, 109, 101, 32, 98, 121, 116, 101, 115, 46};

    static public void main(String args[]) throws Exception {
        FileOutputStream fout = new FileOutputStream("demo/test.txt");

        FileChannel fc = fout.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(1024);

        for (int i = 0; i < message.length; ++i) {
            buffer.put(String.valueOf(message[i]).getBytes());
            buffer.put(new String(",").getBytes());
        }

        buffer.flip();

        fc.write(buffer);

        fc.write(ByteBuffer.wrap(new String("写入数据").getBytes()));
        fout.close();
        Path path = Paths.get("demo/test.txt");
        Files.write(path, "\n追加内容".getBytes(), StandardOpenOption.APPEND);
    }
}
